package com.loyal.loyalerp.basedata.employeechange.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.loyal.loyalerp.basedata.department.model.Department;
import com.loyal.loyalerp.basedata.department.service.DepartmentService;
import com.loyal.loyalerp.basedata.employeechange.model.GyPersonChange;
import com.loyal.loyalerp.basedata.employeechange.service.PersonChangeService;
import com.loyal.loyalerp.commons.ServerResponse;
import com.loyal.loyalerp.humenresource.askleave.model.vo.GridVO;
import com.loyal.loyalerp.mapper.GyPersonMapper;
import com.loyal.loyalerp.model.GyPerson;
import com.loyal.loyalerp.model.GyUser;
import com.loyal.loyalerp.service.EmployeeService;

@Controller
@RequestMapping("personChange")
public class PersonChangeController {
	@Autowired
	PersonChangeService personChangeService;
	@Autowired
	DepartmentService departmentService;
	@Autowired
	EmployeeService userService;
	@Autowired
	GyPersonMapper personMapper;

	@RequestMapping("goEmployeeChangePage")
	public String goEmployeeChangePage() {
		return "basedata/employeeChange/listEmployeeChange";
	}

	@ResponseBody
	@RequestMapping("/listPersonChanges")
	public Object listPersonChanges(@RequestParam(name = "page", defaultValue = "1") Integer page,
			@RequestParam(name = "rows", defaultValue = "5") Integer rows) throws Exception {
		PageHelper.startPage(page, rows);
		List<GyPersonChange> list = personChangeService.getAllPersonChanges();
		PageInfo<GyPersonChange> pageInfo = new PageInfo<GyPersonChange>(list);
		return GridVO.fromPageInfo(pageInfo);
	}

	@RequestMapping("addPersonChangePage")
	public String addPersonChangePage(HttpServletRequest request) {
		request.setAttribute("persons", personMapper.selectAllPerson());
		return "basedata/employeeChange/addEmployeeChange";
	}

	@ResponseBody
	@RequestMapping("add")
	public Object add(GyPersonChange personChange) {
		if (!preAdd(personChange))
			return ServerResponse.createByError("参数不合法！");

		boolean flag = personChangeService.addPersonChange(personChange);
		if (flag) {
			updateOtherTable(personChange);
			return ServerResponse.createBySuccess();
		} else
			return ServerResponse.createByError("插入数据库失败！");
	}

	@ResponseBody
	@RequestMapping("update")
	public Object update(GyPersonChange personChange) {
		if (!preUpdate(personChange))
			return ServerResponse.createByError("参数不合法！");
		boolean flag = personChangeService.updatePersonChange(personChange);
		if (flag) {
			updateOtherTable(personChange);
			return ServerResponse.createBySuccess();
		} else
			return ServerResponse.createByError("更新数据库失败！");
	}

	@ResponseBody
	@RequestMapping("getDepartmentNameByPersonId")
	public Object getDepartmentNameByPersonId(Integer personId) {
		if (null == personId)
			return ServerResponse.createByError("personId不能为null");
		else
			return ServerResponse.createBySuccess(personChangeService.getDepartmentNameByPersonId(personId));
	}

	@ResponseBody
	@RequestMapping("getDepartmentNameByUserId")
	public Object getDepartmentNameByUserId(Integer userId) {
		if (null == userId)
			return ServerResponse.createByError("userId不能为null");
		else
			return ServerResponse.createBySuccess(personChangeService.getDepartmentNameByUserId(userId));
	}

	@ResponseBody
	@RequestMapping("delete")
	public Object delete(Integer id) {
		if (null == id)
			return ServerResponse.createByError("id不能为null");
		boolean flag = personChangeService.deletePersonChange(id);
		if (flag)
			return ServerResponse.createBySuccess("删除成功！");
		else
			return ServerResponse.createByError("删除失败！");
	}

	private void updateOtherTable(GyPersonChange personChange) {
		if (personChange.getChangetype().equals("部门变动")) {
			Department department = departmentService.selectByDeptname(personChange.getAfterchange());
			if (department != null) {
				GyPerson person = new GyPerson();
				person.setId((long) personChange.getPersonid());
				person.setDeptid(department.getId());
				personMapper.updateByPrimaryKeySelective(person);
			}
		}
	}

	private boolean preUpdate(GyPersonChange personChange) {
		Integer personChangeId = personChange.getId();
		if (null == personChangeId)
			return false;
		return preAdd(personChange);
	}

	private boolean preAdd(GyPersonChange personChange) {
		String changeType = personChange.getChangetype();
		Integer personId = personChange.getPersonid();
		Date effectDate = personChange.getEffectdate();
		if (null == effectDate)
			return false;
		if (null == personId || personId == 0)
			return false;
		if (null == changeType || changeType.equals(""))
			return false;
		GyUser user = (GyUser) SecurityUtils.getSubject().getSession().getAttribute("user");
		if (null == user)
			return false;

		personChange.setMakerid(user.getId());
		personChange.setMakedate(new Date());
		switch (personChange.getChangetype()) {
		case "部门变动":
			if (null == personChange.getBeforechange())
				return false;
			if (null == personChange.getAfterchange())
				return false;
			break;
		case "转正":
			personChange.setBeforechange("非正式人员");
			personChange.setAfterchange("正式人员");
			break;
		case "离职":
			personChange.setBeforechange("员工");
			personChange.setAfterchange("非员工");
			break;
		case "入职":
			personChange.setBeforechange("非员工");
			personChange.setAfterchange("员工");
			break;
		}
		return true;
	}
}
